<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">角色名</label>
        <div class="layui-input-block">
            <input type="text" name="roleName" lay-verify="required" lay-reqtext="角色名不能为空" placeholder="请输入用户名" value="" class="layui-input">            
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色描述</label>
        <div class="layui-input-block">
             <textarea placeholder="请输入角色描述" class="layui-textarea" name="roleDetail"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
         <label class="layui-form-label">菜单选择</label>
	       <div class="layui-input-inline">
	           <div id="test1"></div>
	       </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            <button class="layui-btn layui-btn-normal" id="cancel">取消</button>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form','tree'], function () {
        var form = layui.form,
            layer = layui.layer,
            layarea = layui.layarea,
            tree = layui.tree,
            $ = layui.$;
        
        var iframeIndex = parent.layer.getFrameIndex(window.name);
        
      //渲染
      layer.ready(function(){
     	   $.post("../sysMenu/getRoleMenu",{'id':''}, function (data) {
     		  tree.render({
     		        elem: '#test1'  //绑定元素
     		     	,showCheckbox:true
     		     	,id:'menuId'
     		        ,data: data
     	        });
     		});
        });
        
        //监听提交
        form.on('submit(saveBtn)', function (data) {
        	//获得选中的节点
        	var checkData = tree.getChecked('menuId');        	
        	var ids=getCheckedMenuIds(checkData);
        	data.field.menu=ids;
        	$.ajax({
                type : "POST", //提交方式
                url : "../sysRole/save",//路径
                contentType: "application/json;charset=utf-8",
                data : JSON.stringify(data.field),//数据，这里使用的是Json格式进行传输
                dataType: "json",
                success : function(result) {//返回数据根据结果进行相应的处理
                    if (result.code == "0") {
                        var index = layer.msg("成功", {
                            time: 300 //如果不配置，默认是3秒）
                        },function () {
                        	// 关闭弹出层
                            layer.close(index);
                            parent.layer.close(iframeIndex);
                        })
                    }else{
                        layer.msg(result.msg);
                    }
                }
            });
            return false;
        });              
        
        $('#cancel').on("click", function () {
             parent.layer.close(iframeIndex);
        });
    });
    
    var ids = [];
    function getCheckedMenuIds(checkData){
		var length = checkData.length;
    	for(var i=0; i<length; i++){
    		var n = checkData[i];
    		var id = n['id'];
    		ids.push(id);
    		var children=n['children'];
    		if(children !=undefined){
    			getCheckedMenuIds(children);
    		}
    	}
    	return ids;
    }
</script>
</body>
</html>